<%@ page import="bean.Movie" %>
<%@ page import="service.ScreeningScheduleService" %>
<%@ page import="service.impl.ScreeningScheduleServiceImpl" %>
<%@ page import="bean.ScreeningSchedule" %>
<%@ page import="java.util.List" %>
<%@ page import="service.impl.CinemaServiceImpl" %>
<%@ page import="java.util.Date" %>
<%@ page import="util.DataTypeConversion" %><%--
  Created by IntelliJ IDEA.
  User: Peter Zhou
 
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>影片场次选择</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f2f2f2;
        }
        body {
            background-image: url('../img/ui/beijing2.jpg');
            background-size: cover;
            background-repeat: no-repeat;
            background-attachment: fixed; /* 可选，使背景图片固定不动 */
        }

        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            opacity: 0.8;
        }

        .movie-info {
            text-align: center;
            margin-bottom: 20px;
        }

        .movie-info img {
            width: 200px;
            height: auto;
            border-radius: 5px;
        }

        .movie-title {
            font-size: 24px;
            font-weight: bold;
            margin-top: 10px;
        }

        .schedule-list {
            margin-top: 20px;
        }

        .schedule-item {
            margin-bottom: 20px;
            padding: 10px;
            background-color: #f5f5f5;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }

        .schedule-item:hover {
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
        }

        .schedule-info {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .schedule-time {
            font-size: 14px;
            color: #666;
            margin-bottom: 10px;
        }

        .schedule-theater {
            font-size: 14px;
            color: #666;
            margin-bottom: 10px;
        }

        .schedule-seats {
            font-size: 14px;
            color: #666;
            margin-bottom: 10px;
        }

        .schedule-actions {
            text-align: center;
        }

        .schedule-action {
            display: inline-block;
            padding: 5px 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
            color: #333;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-right: 5px;
        }

        .schedule-action:hover {
            background-color: #f5f5f5;
        }

        .pagination {
            text-align: center;
            margin-top: 20px;
        }

        .pagination-link {
            display: inline-block;
            padding: 5px 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
            color: #333;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-right: 5px;
        }

        .pagination-link:hover {
            background-color: #f5f5f5;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="movie-info">
        <% Movie movie =(Movie) request.getSession().getAttribute("movie");
          int movieId = movie.getId();
            ScreeningScheduleService screeningScheduleService = new ScreeningScheduleServiceImpl();
            List<ScreeningSchedule> screeningSchedules = screeningScheduleService.getScreeningSchedulesByMovieId(movieId);
            int sessionNum = 0;
        %>
        <img src="../img/movie/<%=movie.getImg()%>" alt="Movie Image">
        <div class="movie-title"><%=movie.getName()%></div>
    </div>
    <div class="schedule-list">
        <%for(ScreeningSchedule screeningSchedule : screeningSchedules){%>
            <%if(screeningSchedule.getStartTime().after(new Date(System.currentTimeMillis()))){
                sessionNum++;
            %>
        <div class="schedule-item">
            <div class="schedule-info">放映时间：<%=DataTypeConversion.conversionUtilDateToString(screeningSchedule.getStartTime())%></div>
            <div class="schedule-time">散场时间：<%=DataTypeConversion.conversionUtilDateToString(screeningSchedule.getEndTime())%></div>
            <div class ="schedule-theater">影院：<%=(new CinemaServiceImpl().getCinemaById(screeningSchedule.getVideoHall().getCinemaId())).getName()%></div>
            <div class="schedule-theater">放映厅：<%=screeningSchedule.getVideoHall().getName()%></div>
            <div class="schedule-actions">
                <div class="schedule-action"><a href="buy.jsp" onclick="getScreeningScheduleId(<%=screeningSchedule.getId()%>)" >购票</a></div>
            </div>

        </div>
            <%}%>
        <%}%>

        <%if(sessionNum>=1){%>
        <div class="pagination">
            <div class="pagination-link">上一页</div>
            <div class="pagination-link">下一页</div>
        </div><%}else{%>
        <h3 style="text-align: center; color:red;">暂时没有往后的放映安排</h3>
        <%}%>
    </div>
</div>
<script>
    function  getScreeningScheduleId(screeningScheduleId){
        var xhr = new XMLHttpRequest();
        xhr.open("post","get_screeningSchedule",true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        xhr.onreadystatechange = function () {
            // ... 省略了之前的 readyState 检查 ...
            if (xhr.readyState === 4) {
                if (xhr.status === 200) { // 检查响应状态码是否为200（成功）
                    console.log("请求已完成,且响应已就绪");
                    console.log("放映编号"+screeningScheduleId);
                    // 这里可以处理服务器的响应，或者进行页面刷新等操作
                    // 例如，您可能想要更新页面的某个部分，而不是整个页面刷新
                    // 如果确实需要页面刷新或跳转到另一个页面，可以在这里设置 window.location
                } else {
                    console.error("请求失败，状态码：" + xhr.status);
                }
            }
        };
        xhr.send("screeningScheduleId=" + screeningScheduleId); // 发送产品ID到服务器
    }

    document.addEventListener('DOMContentLoaded', function() {
        const scheduleItems = document.querySelectorAll('.schedule-item');
        const paginationLinks = document.querySelectorAll('.pagination-link');
        let currentPage = 0;
        const schedulesPerPage = 4; // 每页显示的场次数量

        showPage(currentPage);

        // 显示指定页面的场次
        function showPage(page) {
            // 隐藏所有场次
            scheduleItems.forEach(function(scheduleItem) {
                scheduleItem.style.display = 'none';
            });

            // 计算起始和结束索引
            const startIndex = page * schedulesPerPage;
            const endIndex = startIndex + schedulesPerPage;

            // 显示指定页面的场次
            for (let i = startIndex; i < endIndex && i < scheduleItems.length; i++) {
                scheduleItems[i].style.display = 'block';
            }
        }


        // 点击下一页按钮
        paginationLinks[1].addEventListener('click', function() {
            if ((currentPage + 1) * schedulesPerPage < scheduleItems.length) {
                currentPage++;
                showPage(currentPage);
            }
        });

        // 点击上一页按钮
        paginationLinks[0].addEventListener('click', function() {
            if (currentPage > 0) {
                currentPage--;
                showPage(currentPage);
            }
        });
    });

</script>
</body>

</html>
